/**
 * Created by baby520 on 2016/8/12.
 */
define(function(require, exports, modules) {
    'use strict';

    require('css!assets/css/records/records.css');
    require('assets/plugins/md5.js');
    require('http://static.geetest.com/static/tools/gt.js');	//极客SDK

    var template = require('text!./tpl.register.html'),
    	registered = require('text!./tpl.registered.html'),
        util = require('util');
    var View = Backbone.View.extend({
        el : '#page',
        events : {
        	'click .radio-btn': 'choseSex',
        	'click #forms .btn-vcode': 'sendCode',
        	'click #confirm1': 'register',
        	'click .btn-enterGame': 'toLogin',
        	'click .black_1': 'back',
        },
        initialize : function() {
            this.template = _.template(template);
            this.registered = _.template(registered);
            this.render();
        },
        render : function() {
            this.$el.html(this.template());
            var userRefId = $.getUrlParam('userRefId');
            if(userRefId){
            	$('input[name="upperUserRefId"]').val(userRefId);
            	$('input[name="upperUserRefId"]').attr('readonly', true);
            }
        },
        choseSex: function(e){
        	$(e.currentTarget).addClass('checkedRadio').siblings('.radio-btn').removeClass('checkedRadio');
        	$(e.currentTarget).children('input').prop("checked",true);
        	$(e.currentTarget).siblings('.radio-btn').children('input').prop("checked",false);
        },
        timer:function($elm){
            var _this=this;
            this.clock = this.clock == undefined ? 60 : this.clock;
            if (this.clock == 0) {
            	$elm.removeClass('sended').text('获取验证码');
                delete this.clock;
            } else {
            	$elm.addClass('sended').text(this.clock + 'S');
            	this.clock = this.clock - 1;
                setTimeout(function() {
                        _this.timer($elm);
                    },1000);
            }
        },
        sendCode: function(e){
        	if($(e.currentTarget).hasClass('sended')){
        		return;
        	}
        	var loginName = $.trim($('input[name="loginName"]').val()),
	    		gender = $.trim($('input[name="sex"]:checked').val()),
	    		userName = $.trim($('input[name="userName"]').val()),
	    		weixin = $.trim($('input[name="weixin"]').val()),
	    		zhifubao = $.trim($('input[name="zhifubao"]').val()),
	    		password = $.trim($('input[name="password"]').val()),
	    		password2 = $.trim($('input[name="password2"]').val()),
	    		phone = $.trim($('input[name="phone"]').val()),
	    		upperUserRefId = $.trim($('input[name="upperUserRefId"]').val());
			if (!loginName) {
				commonAlert_oneBtn('请输入用户名');
				return;
			}
			if (!userName) {
				commonAlert_oneBtn('请输入姓名');
				return;
			}
			if (!weixin) {
				commonAlert_oneBtn('请输入微信');
				return;
			}
			if (!zhifubao) {
				commonAlert_oneBtn('请输入支付宝');
				return;
			}
			if (!password) {
				commonAlert_oneBtn('请输入密码');
				return;
			}
			if (!password2) {
				commonAlert_oneBtn('请输入确认密码');
				return;
			}
			if(password != password2){
        		commonAlert_oneBtn('两次输入的密码不一致');
        		return;
        	}
			if (!phone) {
				commonAlert_oneBtn('请输入手机号');
				return;
			}
			if (!upperUserRefId) {
				commonAlert_oneBtn('请输入邀请码');
				return;
			}
			
			var parm = {
					loginName: loginName,
					gender: gender,
					userName: userName,
					weixin: weixin,
					zhifubao: zhifubao,
					password: hex_md5(password),
					phone: phone,
					upperUserRefId: upperUserRefId
			};
			
			var _this = this;
        	loadingShow();
        	$('body').append('<div class="gt"></div>');
        	var handlerPopupMobile = function (captchaObj) {
        		captchaObj.appendTo(".gt");
        		captchaObj.onSuccess(function () {
        			$(".gt").remove();
        			var validate = captchaObj.getValidate();
                    jsonpGet(util.NDNserver + "/getVcode",                        
        					{
        						jsonStr: JSON.stringify(parm),
        						geetest_challenge: validate.geetest_challenge,
        						geetest_validate: validate.geetest_validate,
        						geetest_seccode: validate.geetest_seccode
        					},
        					'getVcode',
        					function (res) {
        						$(".common-loading").remove();
        						if(res.code==1){
        							_this.timer($('#forms .btn-vcode'));
        							commonAlert_oneBtn('发送成功');
        							_this.actionId = res.data;
        						}else{
        							commonAlert_oneBtn(res.msg);
        						}
        					}
        			);
        		});
        	};
        	jsonpGet(
        			util.NDNserver + "/geetestRegister", 
        			{t: new Date().getTime()},
        			'geetestRegister',
        			function (json) {
        				var data = $.parseJSON(json.data);
        				initGeetest({
        					gt: data.gt,
        					challenge: data.challenge,
        					offline: !data.success
        				}, handlerPopupMobile);
        			}
        	);
        },
        register: function(){
        	var _this = this;
        	var vcode = $.trim($('input[name="vcode"]').val());
        	var loginName = $.trim($('input[name="loginName"]').val()),
	    		userName = $.trim($('input[name="userName"]').val()),
	    		password = $.trim($('input[name="password"]').val()),
	    		password2 = $.trim($('input[name="password2"]').val()),
	    		phone = $.trim($('input[name="phone"]').val()),
	    		upperUserRefId = $.trim($('input[name="upperUserRefId"]').val());
        	
        	if(!vcode || !loginName || !userName || !password || !password2 || !phone || !upperUserRefId){
        		//commonAlert_oneBtn('请输入验证码');
        		return;
        	}
        	if(!this.actionId){
        		commonAlert_oneBtn('请先获取验证码');
        		return;
        	}
        	jsonpGet(util.NDNserver+ "/openRegister",
					{
						actionId: this.actionId,
						vCode: vcode
					},
					'userRegister',
					function (res) {
						$(".common-loading").remove();
						if(res.code==1){
							$('#page').html(_this.registered);
						}else{
							commonAlert_oneBtn(res.msg);
						}
					}
			);
        },
        toLogin: function(){
        	Backbone.history.navigate('login',{trigger:true});
        },
        back:function(){
        	Backbone.history.navigate('entry',{trigger:true});
        }
        
    });

    return View;
});